Option Strict On

Imports System.Data.SqlClient
Imports System.Text.RegularExpressions


Public Class Tagging
    Inherits BusinessBase

#Region "Config Options"
    Public _normalize_tags As Boolean
    Public _normalized_valid_chars As String = "a-zA-Z0-9"
    Public _block_multiuser_tag_on_object As Integer = 1
    Public _append_to_integer As String = ""
    Public _PCONNECT As Boolean = False
    Public _MAX_TAG_LENGTH As Integer = 30

#End Region

#Region "Instance Properties"
    Public TagItemList As New List(Of TagItem)
#End Region

#Region "Add Tags"

    ''' <summary>
    ''' Tags the document.
    ''' </summary>
    ''' <param name="tags">The tags.</param>
    ''' <param name="Identifier">The identifier.</param>
    ''' <param name="user">The user.</param>
    Public Sub TagDocument(ByVal tags As String, ByVal Identifier As Guid, Optional ByVal user As String = "")
        TagDocument(ParseTags(tags), Identifier, user)
    End Sub

    ''' <summary>
    ''' Tags the document.
    ''' </summary>
    ''' <param name="tags">The tags.</param>
    ''' <param name="Identifier">The identifier.</param>
    ''' <param name="user">The user.</param>
    Private Sub TagDocument(ByVal tags As List(Of String), _
                            ByVal Identifier As Guid, _
                            Optional ByVal user As String = "")
        For Each tag As String In tags
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagAddNew", _
                                       New SqlParameter("@Tag", tag), New SqlParameter("Identifier", Identifier), _
                                       New SqlParameter("user", user))
        Next
    End Sub

#End Region

#Region "Get Tags"

    Public Function GetDocumentAuthors() As List(Of TagItem)
        Dim tags As New List(Of TagItem)
        Using _
            reader As SqlDataReader = _
                SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, _
                                         "DocumentTopAuthors")
            While reader.Read
                tags.Add( _
                          New TagItem(CStr(reader("AuthorLast")), CInt(reader("Count")), _
                                       String.Format("/studyguide.aspx?action=author&Id={0}", reader("AuthorId"))))
            End While
        End Using

        Return tags
    End Function

    Public Shared Function GetDocumentTags(ByVal Identifier As Guid) As List(Of Tag)
        Dim list As New List(Of Tag)
        If Identifier = Guid.Empty Then Return list
        Using _
            reader As SqlDataReader = _
                SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetDocumentTags", _
                                         New SqlParameter("Identifier", Identifier))
            While reader.Read
                Dim tag As New Tag
                tag.TagId = ObjToString(reader("TagId"))
                tag.Tag = ObjToString(reader("Tag"))
                tag.TagCount = CInt(ObjToString(reader("Count")))
                'tag.TagAuthority = DataFormat.ObjToString(reader("TagAuthority"))
                'tag.EditDate = CDate(reader("EditDate"))
                list.Add(tag)
            End While
        End Using
        Return list
    End Function

    Public Function GetUserTags(ByVal User As String, Optional ByVal User2 As String = "") As List(Of Document)
        Dim documents As New List(Of Document)
        Using _
            reader As SqlDataReader = _
                SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetUserTags", _
                                         New SqlParameter("User", User), New SqlParameter("User2", User2))
            While reader.Read
                Dim document As New Document
                document.Identifier = CType(reader("GUID"), Guid)
                document.Title = CStr(reader("Title"))
                document.Description = CStr(reader("Tag"))
                documents.Add(document)
            End While
            reader.NextResult()

            ' Get Tag Cloud
            While reader.Read
                TagItemList.Add( _
                          New TagItem(CStr(reader("Tag")), CInt(reader("Count")), _
                                       String.Format("/tag/{0}", reader("Tag"))))
            End While

        End Using
        Return documents
    End Function

    Public Shared Function GetDocumentTagCloud(ByVal Identifier As Guid) As List(Of TagItem)
        Dim tags As New List(Of TagItem)
        Using _
            reader As SqlDataReader = _
               SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetDocumentTags", _
                                         New SqlParameter("Identifier", Identifier))
            While reader.Read
                tags.Add( _
                          New TagItem(CStr(reader("Tag")), CInt(reader("Count")), _
                                       String.Format("/tag/{0}", reader("Tag"))))
            End While
        End Using

        Return tags
    End Function


    Public Shared Function GetDocumentsWithTag(ByVal tags As String) As List(Of Document)

        Dim documents As New List(Of Document)

        Using reader As SqlDataReader = SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetDocumentsWithTags", New SqlParameter("Tags", tags))
            While reader.Read
                Dim document As New Document
                document.Id = ObjToInteger(reader("Id"))
                document.Identifier = CType(reader("GUID"), Guid)
                document.Title = CStr(reader("Title"))
                document.SetDocumentType(CStr(reader("Type")))
                documents.Add(document)
            End While
        End Using
        Return documents
    End Function

    Public Shared Function GetRelatedTagCloud(ByVal tag As String) As List(Of TagItem)
        Dim tags As New List(Of TagItem)
        Using _
            reader As SqlDataReader = _
               SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetRelatedTags", New SqlParameter("@Tag", tag))
            While reader.Read
                tags.Add( _
                          New TagItem(CStr(reader("Tag")), CInt(reader("Count")), _
                                       String.Format("/tag/{0}", reader("Tag"))))
            End While
        End Using

        Return tags
    End Function

    Public Shared Function GetPopularTagCloud() As List(Of TagItem)
        Dim tags As New List(Of TagItem)
        Using _
            reader As SqlDataReader = _
               SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "TagGetTopTags")
            While reader.Read
                tags.Add( _
                          New TagItem(CStr(reader("Tag")), CInt(reader("Count")), _
                                       String.Format("/tag/{0}", reader("Tag")))) ' tags.aspx?tag={0} without rewrite
            End While
        End Using

        Return tags
    End Function


    'Public Shared Function GetDocumentsWithTags(ByVal tags As List(Of String)) As List(Of Document)
    '    Dim list As New List(Of String)
    '    list.Add(tag)
    '    Return GetDocumentsWithTags(list)
    'End Function

#End Region

#Region "Edit Tags"

    Public Shared Function DeleteTag(ByVal tag As String) As Integer
        Return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagDelete", New SqlParameter("Tag", tag))
    End Function

    Public Shared Function DeleteDocumentTag(ByVal tag As String, ByVal Identifier As Guid) As Integer
        SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagDelete", New SqlParameter("Tag", tag), New SqlParameter("Identifier", Identifier.ToString))
    End Function


    Public Shared Function DeleteAllDocumentTags(ByVal Identifier As Guid) As Integer
        SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagDelete", New SqlParameter("Identifier", Identifier.ToString))
    End Function

    ''' <summary>
    ''' Deletes the user tag for the given document GUID
    ''' </summary>
    ''' <param name="user">The user.</param>
    ''' <param name="Identifier">The identifier.</param>
    Public Shared Sub DeleteUserDocumentTag(ByVal user As String, ByVal Identifier As Guid, ByVal tag As String)
        SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagDelete", New SqlParameter("Identifier", Identifier), New SqlParameter("user", user), New SqlParameter("tag", tag))
    End Sub

    Public Shared Function DeleteUserTags(ByVal user As String) As Integer
        Return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "TagDelete", New SqlParameter("user", user))
    End Function

    Public Shared Function UpdateTag(ByVal oldTag As String, ByVal newTag As String) As Integer
        Return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, "UPDATE Tag SET Tag = @newTag WHERE Tag = @oldTag", New SqlParameter("oldTag", oldTag), New SqlParameter("newTag", newTag))
    End Function

#End Region

#Region "Format Tags"

    Private Function ParseTags(ByVal tags As String) As List(Of String)
        ParseTags = New List(Of String)
        Dim tagArray As String() = DataFormat.StripHTML(tags).Split(CChar(","))
        For Each tag As String In tagArray
            ParseTags.Add(NormalizeTag(tag.Trim))
        Next
        Return ParseTags
    End Function

    Private Function NormalizeTag(ByVal tag As String) As String
        Dim normalized_valid_chars As String
        Dim normalized_tag As String
        If Convert.ToBoolean(Me._normalize_tags) Then
            normalized_valid_chars = Me._normalized_valid_chars
            normalized_tag = New Regex("/[^" + normalized_valid_chars + "]/").Replace(tag, "")
            Return normalized_tag.ToLower()
        Else
            Return tag
        End If
    End Function




#End Region


End Class
